package com.atguigu.gmall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/***
 * 订单的延时队列配置
 */
@Configuration
public class OrderDelayRabbitMqConfig {

    /**
     * 订单正常交换机
     * @return
     */
    @Bean("orderNormalExchange")
    public Exchange orderNormalExchange(){
        return ExchangeBuilder.directExchange("order_normal_exchange").build();
    }

    /**
     * 订单正常队列
     */
    @Bean("orderNormalQueue")
    public Queue orderNormalQueue(){
        return QueueBuilder
                .durable("order_normal_queue")
                .withArgument("x-dead-letter-exchange","order_dead_exchange")
                .withArgument("x-dead-letter-routing-key","order.dead")
                .build();
    }

    /**
     * 订单正常队列与订单正常交换机绑定
     */
    @Bean
    public Binding orderNormalBinding(@Qualifier("orderNormalExchange") Exchange exchange,
                                      @Qualifier("orderNormalQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("order.normal").noargs();
    }

    /**
     * 订单死信交换机
     */
    @Bean("orderDeadExchange")
    public Exchange orderDeadExchange(){
        return ExchangeBuilder.directExchange("order_dead_exchange").build();
    }

    /**
     * 订单死信队列
     */
    @Bean("orderDeadQueue")
    public Queue orderDeadQueue(){
        return QueueBuilder.durable("order_dead_queue").build();
    }

    /**
     * 订单死信队列与订单死信交换机绑定
     */
    @Bean
    public Binding orderDeadBinding(@Qualifier("orderDeadExchange") Exchange exchange,
                                    @Qualifier("orderDeadQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("order.dead").noargs();
    }
}
